package com.zhihu_lvliangi_android.option.zhengwu;

import java.util.ArrayList;
import java.util.List;

import com.android.widget.listview.pullload.PullLoadListView;
import com.android.widget.listview.pullload.PullLoadListView.OnRefreshListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.R.layout;
import com.zhihu_lvliangi_android.domain.JsonPageModel;
import com.zhihu_lvliangi_android.domain.ZhengwuColumnItem;
import com.zhihu_lvliangi_android.domain.ZhengwuGuideItem;
import com.zhihu_lvliangi_android.option.parent.ParentActivity;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.zhengwu.adapter.NewsListViewAdapter;
import com.zhihu_lvliangi_android.option.zhengwu.adapter.ZhengWuItemListAdapter;
import com.zhihu_lvliangi_android.service.DataFormat;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.service.TaskID;
import com.zhihu_lvliangi_android.service.ClientTask.LoadingType;
import com.zhihu_lvliangi_android.util.Constant;
import com.zhihu_lvliangi_android.util.RequestURL;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;

/***
 * 事项列表
 * @author 徐宇健
 *
 */
public class Category_Item_List_Activity extends ParentActivity {

	private JsonPageModel<ZhengwuGuideItem> mItem;
	private PullLoadListView mPullListView;
	private ZhengWuItemListAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		String json = getIntent().getStringExtra(Constant.GENERAL_SAVE_KEY);
		Gson gson =new Gson();
		 mItem = gson.fromJson(json, new TypeToken<JsonPageModel<ZhengwuGuideItem>>(){}.getType());
		System.out.println(mItem.getResult().getRows().size());
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar));
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setIcon(new BitmapDrawable());//不设置图标icon
		
	}
	
	@Override
	protected int getLayoutId() {
		return R.layout.activity_category__item__list;
	}

	@Override
	protected void setupViews() {
		initActionBar();
		mPullListView = (PullLoadListView) findViewById(R.id.news_item_ListView);
		mPullListView.setCanRefresh(true);
		adapter = new ZhengWuItemListAdapter(this, mItem.getResult().getRows());
		mPullListView.setAdapter(adapter);
		if (mItem.getResult().getRows() == null||mItem.getResult().getRows().size() ==0) {
			adapter.setmItems(new ArrayList<ZhengwuGuideItem>());
			showShortToast("该分类下没有办事指南!");
		}
		mPullListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				MobileApplication.getInstance().clientTask.executeGet(TaskID.TASK_APP_ZHENGWU_GUIDE_ITEM
						, DataFormat.STRING, getIntent().getStringExtra("URL")
						, Category_Item_List_Activity.this);
				MobileApplication.getInstance().clientTask.setLoadingType(LoadingType.CUSTUM_PRIVATE);
			}
		});
		mPullListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (--arg2 >=0) {
					Intent intent = new Intent(Category_Item_List_Activity.this, AffairDetailActivity.class);
					intent.putExtra(Constant.GENERAL_SAVE_KEY, mItem.getResult().getRows().get(arg2));
					startActivity(intent);
				}
				
			}
		});
	}

	@Override
	protected void initialized() {
		
	}

	@Override
	protected void threadTask() {
		
	}

	@Override
	public void onResponse(Object response, Enum<?> reqId) {
		super.onResponse(response, reqId);
		mPullListView.onRefreshComplete();
		String url = getIntent().getStringExtra("URL");
		MobileApplication.cacheUtils.put(url.substring(url.lastIndexOf("=")+1),response.toString());
		mItem = gson.fromJson(response.toString(), new TypeToken<JsonPageModel<ZhengwuGuideItem>>(){}.getType());
		List<ZhengwuGuideItem> items = mItem.getResult().getRows();
		if (items == null||items.size() ==0) {
			adapter.setmItems(new ArrayList<ZhengwuGuideItem>());
			showShortToast("该分类下没有办事指南!");
		}
		adapter.setmItems(items);
		adapter.notifyDataSetChanged();
	}

	
	

}
